package cc.ucanuup.service.base.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cc.ucanuup.service.base.entity.UpResource;

/**
 * Description: TODO[描述该类概要功能介绍]
 *
 * @author guwenbin
 * @date 2018年2月3日
 * @version 1.0
 */
public interface UpResourceJPA  extends JpaRepository<UpResource, Serializable>,JpaSpecificationExecutor<UpResource>{

	@Query(value="SELECT T FROM UpResource T where  T.deleted = 0 AND T.fid like :fid order by T.resName asc",countQuery="SELECT COUNT(1) FROM UpResource T where T.deleted = 0 AND T.fid like :fid")
	Page<UpResource> queryResourcesAndSortItByFid(@Param("fid")String fid, Pageable pageable);


	/**
	 * @Method: com.bozhon.service.base.datamanager.ResourceJPA.findResourceByFid
	 * @Description: 通过菜单id查找数据类型
	 * @author: WenBin
	 * @date: 2017年10月16日
	 * @version: 1.0
	 * @param fid
	 * @param pageable
	 * @return
	 * List<Resource>
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Query(value="SELECT T FROM UpResource T where  T.deleted = 0 AND T.fid = :fid order by T.resName asc",countQuery="SELECT COUNT(1) FROM UpResource T where T.deleted = 0 AND T.fid = :fid")
	Page<UpResource> queryResourceByFid(@Param("fid")String string, Pageable pageable);

	/**
	 * @Method: com.bozhon.service.base.datamanager.ResourceJPA.findResourceByRoleIdAndFid
	 * @Description: 通过父ID 以及角色ID 查询  权限资源
	 * @author: WenBin
	 * @date: 2017年10月16日
	 * @version: 1.0
	 * @param fid
	 * @param roleId
	 * @return
	 * List<UpResource>
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Query(value=" SELECT TRL.* FROM TBUP_RESOURCE TRL "
			+" LEFT JOIN  RELATION_ROLE_RESOURCE  RRR ON RRR.RESOURCE_ID = TRL.ID "
			+" WHERE RRR.ROLE_ID = :roleId AND TRL.FID = :fid ",nativeQuery=true)
	List<UpResource> findResourceByRoleIdAndFid(@Param("fid") String fid,@Param("roleId") String roleId);

	/**
	 * @Method: com.bozhon.srm.service.base.datamanager.UpResourceJPA.findDefaultResourceWithType
	 * @Description: 查找所有的默认自全权限
	 * @author: WenBin
	 * @date: 2018年2月7日
	 * @version: 1.0
	 * @param roleType
	 * @return
	 * List<UpResource>
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Query(value=" SELECT  TRES.* FROM TBUP_RESOURCE TRES  "
			+ "  LEFT JOIN RELATION_ROLE_RESOURCE SRR  ON TRES.ID = SRR.RESOURCE_ID  "
			+ "  LEFT JOIN TBUP_ROLE TR ON SRR.ROLE_ID = TR.ID  "
			+ "  WHERE TR.POWER_TYPE = ?1 ",nativeQuery=true)
	List<UpResource> findDefaultResourceWithType(String roleType);

	/**
	 *
	 * @Method: com.bozhon.srm.service.base.datamanager.UpResourceJPA.findAllActiveResource
	 * @Description:  查找出所有未删除的资源权限
	 * @author: WenBin
	 * @date: 2018年2月7日
	 * @version: 1.0
	 * @return
	 * List<UpResource>
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Query(value="select t from UpResource t where t.deleted = 0 ")
	List<UpResource> findAllActiveResource();

	/**
	 * @Method: com.bozhon.srm.service.base.datamanager.UpResourceJPA.findAllActiveResourceByUserAccount
	 * @Description: 通过账号查询用户的资源信息
	 * @author: WenBin
	 * @date: 2018年2月7日
	 * @version: 1.0
	 * @param userAccount
	 * @return
	 * List<UpResource>
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@Query(value="SELECT  TRES.* FROM TBUP_ROLE TR "
			+ " LEFT JOIN RELATION_ROLE_RESOURCE SRR ON SRR.ROLE_ID = TR.ID "
			+ " LEFT JOIN TBUP_RESOURCE TRES ON TRES.ID = SRR.RESOURCE_ID "
			+ " LEFT JOIN RELATION_USER_ROLE SSR ON SSR.ROLE_ID = TR.ID"
			+ " LEFT JOIN TBUP_USER TS ON TS.ID = SSR.USER_ID"
			+ " WHERE TS.USER_ACCOUNT = ?1 "
			+ " AND TR.DELETED = 0 AND SRR.DELETED = 0  AND TRES.DELETED = 0  AND SSR.DELETED = 0 AND TS.DELETED = 0 ",nativeQuery=true)
	List<UpResource> findAllActiveResourceByUserAccount(String userAccount);

}
